package com.tenglong.teacher.ui.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.MyApplication;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.ClientUserInfo;
import com.tenglong.teacher.data.resultInfo.DataState;
import com.tenglong.teacher.ui.adapter.LoginSelectOrganizationAdapter;
import com.tenglong.teacher.ui.common.activity.BaseActivity;
import com.tenglong.teacher.ui.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;


public class LoginSelectOrganizationActivity extends BaseActivity implements LoginSelectOrganizationAdapter.LoginSelectOrganizationListener {

    public static final String KEY_SWITCH_ORGANIZATION_TYPE = "key_switch_organization";
    public static final int TYPE_LOGIN_SWITCH_ORGANIZATION = 0;
    public static final int TYPE_USER_SWITCH_ORGANIZATION = 1;


    private RecyclerView recycler;
    private List<ClientUserInfo> infos;
    private LoginSelectOrganizationAdapter mAdapter;
    private LoginT loginT;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_select_organization);
        initView();
        initData();
        setAdapter();
        setListeners();
    }

    private void initData() {
        type = getIntent().getIntExtra(KEY_SWITCH_ORGANIZATION_TYPE, TYPE_LOGIN_SWITCH_ORGANIZATION);
        infos = new ArrayList<>();
        List<ClientUserInfo> userInfos = DataState.getInstance().getUserInfos();
        if (userInfos != null && userInfos.size() > 0) {
            infos.addAll(userInfos);
        }
        ClientUserInfo info = new ClientUserInfo();
        info.isSelectOrganizationTitle = true;
        info.selectOrganizationTitle = "请选择你要加入的机构";
        infos.add(0, info);
    }

    private void setListeners() {
        mAdapter.setLoginSelectOrganizationListener(this);
    }

    private void setAdapter() {
        mAdapter = new LoginSelectOrganizationAdapter(infos);
        recycler.setAdapter(mAdapter);
    }

    private void initView() {
        setTitle("选择机构");
        recycler = findViewById(R.id.recycler);
    }

    @Override
    public void onLoginSelectOrganizationListener(int position, ClientUserInfo info) {
        DataState.getInstance().setOrganizationIndex(position - 1);
        if (isLoginType()) {
            toLoginSwitch(info);
        } else {
            userSwitch(info);
        }
    }

    private void userSwitch(ClientUserInfo info) {
        String url = info.getUrl();
        ClientUserInfo userInfo = DataState.getInstance().getUserInfo();
        if (url == null || url.equals("") || url.endsWith(":8099") || userInfo.isDetaiInfo) {
            userInfo.isDetaiInfo = true;
            WYLogin wyLogin = new WYLogin(new WYLogin.HunXinListener() {
                @Override
                public void onSucceed() {
                    dismissProgressDialog();
                    setResult(Activity.RESULT_OK);
                    finish();
                }

                @Override
                public void onFailed() {
                    dismissProgressDialog();
                }

                @Override
                public void onChangeParams() {
                }
            });
            showProgressDialog("机构切换中...");
            wyLogin.loginNim();
            return;
        }
        loginT = new LoginT(this, new LoginT.LoginTListener() {
            @Override
            public void showLoadingDialog() {
                LoginSelectOrganizationActivity.this.showProgressDialog("加载中...");
            }

            @Override
            public void dissLoadingDialog() {
                LoginSelectOrganizationActivity.this.dismissProgressDialog();
            }

            @Override
            public void showToast(String message) {
                ToastUtils.showCenterToast(message);
            }

            @Override
            public void loginSucceed() {
                setResult(Activity.RESULT_OK);
                finish();
            }

            @Override
            public void loginFailed() {
                LoginSelectOrganizationActivity.this.dismissProgressDialog();
            }
        });
        loginT.getUserInfos();
    }


    private void toLoginSwitch(ClientUserInfo userInfo) {
        String url = userInfo.getUrl();
        if (url == null || url.equals("") || url.endsWith(":8099")) {
            DataState.getInstance().getUserInfo().isDetaiInfo = true;
            WYLogin wyLogin = new WYLogin(new WYLogin.HunXinListener() {
                @Override
                public void onSucceed() {
                    dismissProgressDialog();
                    skipHome();
                    finish();
                    Activity loginClass = MyApplication.mActivityManage.findActivity(LoginActivity.class);
                    if (loginClass != null) {
                        loginClass.finish();
                    }
                }

                @Override
                public void onFailed() {
                    dismissProgressDialog();
                }

                @Override
                public void onChangeParams() {
                }
            });
            showProgressDialog("加载中...");
            wyLogin.loginNim();
            return;
        }
        loginT = new LoginT(this, new LoginT.LoginTListener() {
            @Override
            public void showLoadingDialog() {
                LoginSelectOrganizationActivity.this.showProgressDialog("加载中...");
            }

            @Override
            public void dissLoadingDialog() {
                LoginSelectOrganizationActivity.this.dismissProgressDialog();
            }

            @Override
            public void showToast(String message) {
                ToastUtils.showCenterToast(message);
            }

            @Override
            public void loginSucceed() {
                SharedPreferences.Editor editor = getSharedPreferences("preference", 0).edit();
                editor.putBoolean(StringUtils.USER_LOGIN_STATE, true);
                editor.commit();
                skipHome();
                finish();
                Activity loginClass = MyApplication.mActivityManage.findActivity(LoginActivity.class);
                if (loginClass != null) {
                    loginClass.finish();
                }
            }

            @Override
            public void loginFailed() {
            }
        });
        loginT.getUserInfos();
    }

    private void skipHome() {
        Intent intent = new Intent("android.intent.action.home");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (isLoginType()) {
            DataState.getInstance().appExit();
        }
    }

    public boolean isLoginType(){
        return type == TYPE_LOGIN_SWITCH_ORGANIZATION;
    }
}
